{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-16T12:26:34.007590Z",
     "start_time": "2025-09-16T12:26:33.978393Z"
    }
   },
   "source": [
    "# 相当于debug\n",
    "# assert 1 == 2, 'assertion is wrong'\n",
    "\n",
    "# def apply_discount(price,discount):\n",
    "#     updated_price = price * (1 -discount)\n",
    "#     assert 0 <= updated_price <= price, 'price should be greater or equal to 0 and less or equal to original price'\n",
    "#     return updated_price\n",
    "\n",
    "# def read_and_process(path):\n",
    "#     try:\n",
    "#         with open(path) as f:\n",
    "#     except Exception as e:\n",
    "\n",
    "# with 语句\n",
    "# for x in range(1000000):\n",
    "#     with open('test.txt', 'w') as f:\n",
    "#         f.write('hello')\n",
    "# try:\n",
    "#     with open('my_file.txt', 'r') as f:\n",
    "#         print('文件已打开，正在写入...')\n",
    "#         f.write('hello world!')\n",
    "#\n",
    "#     print('with 代码块结束，文件已自动关闭')\n",
    "#\n",
    "# except Exception as e:\n",
    "#     print(f\"在 with 代码块外部捕获到异常: {e}\")\n",
    "#\n",
    "# print(f.closed)\n",
    "import time\n",
    "\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "在 with 代码块外部捕获到异常: [Errno 2] No such file or directory: 'my_file.txt'\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'f' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[31m---------------------------------------------------------------------------\u001B[39m",
      "\u001B[31mNameError\u001B[39m                                 Traceback (most recent call last)",
      "\u001B[36mCell\u001B[39m\u001B[36m \u001B[39m\u001B[32mIn[2]\u001B[39m\u001B[32m, line 28\u001B[39m\n\u001B[32m     25\u001B[39m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mException\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m e:\n\u001B[32m     26\u001B[39m     \u001B[38;5;28mprint\u001B[39m(\u001B[33mf\u001B[39m\u001B[33m\"\u001B[39m\u001B[33m在 with 代码块外部捕获到异常: \u001B[39m\u001B[38;5;132;01m{\u001B[39;00me\u001B[38;5;132;01m}\u001B[39;00m\u001B[33m\"\u001B[39m)\n\u001B[32m---> \u001B[39m\u001B[32m28\u001B[39m \u001B[38;5;28mprint\u001B[39m(\u001B[43mf\u001B[49m.closed)\n",
      "\u001B[31mNameError\u001B[39m: name 'f' is not defined"
     ]
    }
   ],
   "execution_count": 2
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
